package com.l.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

public class BuildcodeUtil {
	/**
	 * Luckycall发送短信验证码
	 * 
	 * @param mobile
	 *            手机号
	 * @param code
	 *            验证码
	 * @param tp
	 *            验证码类型(传int类型) 				
	 *            1.身份验证验证码(验证码${code}，您正在进行身份验证，打死不要告诉别人哦！),
	 *            2.登录确认验证码(验证码${code}，您正在登录，若非本人操作，请勿泄露),
	 *            3.登录异常验证码(验证码${code}，您正尝试异地登录，若非本人操作，请勿泄露。),
	 *            4.用户注册验证码(验证码${code}，您正在注册成为新用户，感谢您的支持！),
	 *            5.修改密码验证码(验证码${code}，您正在尝试修改登录密码，请妥善保管账户信息。),
	 *            6.信息变更验证码(验证码${code}，您正在尝试变更重要信息，请妥善保管账户信息。)
	 * @return
	 * @throws ServerException
	 * @throws ClientException
	 */
	public static String buildcode(String mobile, String code, int tp) throws Exception {

		System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
		System.setProperty("sun.net.client.defaultReadTimeout", "10000");
		final String product = "Dysmsapi";
		final String domain = "dysmsapi.aliyuncs.com";
		final String accessKeyId = "LTAInHpKvkqcarsY";
		final String accessKeySecret = "gmkdjSqD5Za173hsMfpXwvbHVVpfQ7";
		IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
		DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
		IAcsClient acsClient = new DefaultAcsClient(profile);
		SendSmsRequest request = new SendSmsRequest();
		request.setMethod(MethodType.POST);
		request.setPhoneNumbers(mobile);
		request.setSignName("幸运的电话簿");
		if (tp == 1) {
			request.setTemplateCode("SMS_130980229");
		} else if (tp == 2) {
			request.setTemplateCode("SMS_130980228");
		} else if (tp == 3) {
			request.setTemplateCode("SMS_130980227");
		} else if (tp == 4) {
			request.setTemplateCode("SMS_130980226");
		} else if (tp == 5) {
			request.setTemplateCode("SMS_130980225");
		} else if (tp == 6) {
			request.setTemplateCode("SMS_130980224");
		}
		request.setTemplateParam("{\"name\":\"Tom\", \"code\":" + code + "}");
		SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
		if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
			return sendSmsResponse.getMessage();
		}
		return null;
	}

}
